home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / programm.ing / ams__l~1.zoo / src / soundenv.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-25  |  2.8 KB  |  129 lines

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  This file is part of the Atari Machine Specific Library,
  4. //  and is Copyright 1992 by Warwick W. Allison.
  5. //
  6. //  You are free to copy and modify these sources, provided you acknowledge
  7. //  the origin by retaining this notice, and adhere to the conditions
  8. //  described in the file COPYING.
  9. //
  10. //////////////////////////////////////////////////////////////////////////////
  11.  
  12. #include "Sound.h"
  13. #include "SoundEnvelope.h"
  14. #include <values.h>
  15.  
  16. const ExtendedVolumeResolution=8; 
  17. const short Simple=0;
  18. const short Plotted=1;
  19. short ActiveSounds=0;
  20.  
  21. struct SoundDataRec {
  22.         short Type;
  23.         short Priority,DPriority;
  24.         short Pitch,DPitch;
  25.         short Volume,DVolume;
  26.         WavePoint* Data;
  27.         short filler[7]; // Fill to 16 words
  28.  
  29.         SoundDataRec() :
  30.             Priority(0)
  31.         {}
  32.     } SoundData[3];
  33.  
  34. static
  35. bool GetChannel(short& Ch, short ApplicantPriority)
  36. {
  37.     short Meagreness;
  38.  
  39.     Meagreness=MAXSHORT;
  40.     for (short Try=0; Try<3; Try++) {
  41.         if (SoundData[Try].Priority <= Meagreness) {
  42.             Ch=Try;
  43.             Meagreness=SoundData[Try].Priority;
  44.         }
  45.     }
  46.     if (Meagreness < ApplicantPriority) {
  47.         if (Meagreness>0) {
  48.             ActiveSounds--;
  49.         }
  50.         return TRUE;
  51.     } else {
  52.         return FALSE;
  53.     }
  54. }
  55.  
  56.  
  57. SoundEnvelope::SoundEnvelope(short StartPitch, short PitchChange,
  58.                 short StartVolume,short VolumeChange,
  59.                 short StartPriority,short PriorityChange,
  60.                 bool Noisy) :
  61.     P(StartPitch),dP(PitchChange),
  62.     V(StartVolume << ExtendedVolumeResolution),dV(VolumeChange),
  63.     Pr(StartPriority),dPr(PriorityChange),
  64.     N(Noisy), Wave(FALSE)
  65. { }
  66.  
  67. SoundEnvelope::SoundEnvelope(WavePoint* WaveForm,
  68.                 short StartPriority,short PriorityChange,
  69.                 bool Noisy) :
  70.     Pr(StartPriority), dPr(PriorityChange),
  71.     N(Noisy),
  72.     D(WaveForm), Wave(TRUE)
  73. { }
  74.  
  75. void SoundEnvelope::Start()
  76. /* Initiate a sound.  Terminates when any<=0 or when overridden. */
  77. {
  78.     short Ch;
  79.  
  80.     if (GetChannel(Ch,Pr)) {
  81.         SoundData[Ch].Priority=Pr;
  82.         SoundData[Ch].DPriority=dPr;
  83.         if (Wave) {
  84.             SoundData[Ch].Type=Plotted;    
  85.             SoundData[Ch].Data=D;
  86.         } else {
  87.             SoundData[Ch].Type=Simple;
  88.             SoundData[Ch].Pitch=P;
  89.             SoundData[Ch].DPitch=dP;
  90.             SoundData[Ch].Volume=V;
  91.             SoundData[Ch].DVolume=dV;
  92.         }
  93.         SetVolume(Ch,0);
  94.         SetActive(Ch,TRUE);
  95.         SetNoisy(Ch,N);
  96.         ActiveSounds++;
  97.     }
  98. }
  99.  
  100.  
  101.  
  102. short DoSounds()
  103. /* progress sound output, returns number playing */
  104. {
  105.     for (short Ch=0; Ch<3; Ch++) {
  106.         SoundDataRec* S=&SoundData[Ch];
  107.         if (S->Priority>0) {
  108.             if (S->Type==Simple) {
  109.                 Sound(Ch,S->Pitch,S->Volume >> ExtendedVolumeResolution);
  110.                 S->Pitch=S->Pitch+S->DPitch;
  111.                 S->Volume=S->Volume+S->DVolume;
  112.                 if (S->Pitch<0 || S->Volume<0) S->Priority=0;
  113.             } else {
  114.                 if (S->Data->Pitch<0) S->Priority=0;
  115.                 else {
  116.                     Sound(Ch,S->Data->Pitch,S->Data->Volume);
  117.                     S->Data++;
  118.                 }
  119.             }
  120.             S->Priority=S->Priority+S->DPriority;
  121.             if (S->Priority<=0) {
  122.                 SetVolume(Ch,0);
  123.                 ActiveSounds--;
  124.             }
  125.         }
  126.     }
  127.     return ActiveSounds;
  128. }
  129.